﻿using System;
using Model;
using Service;
using Resources;


public partial class preventiveAdd : System.Web.UI.Page
{
    Utility _Utility = new Utility();
   

    protected void Page_Load(object sender, EventArgs e)
    {

      
        if (!IsPostBack)
        {
            lblCodeNumber.Text = " Add new  preventive  ";
            pnlBody.Visible = true;
        }
    }
    
    private Preventive Getpreventive()
    {​
        
        Preventive preventive = new Preventive();

        preventive.EmployeeId = Int32.Parse(drlEmployee.SelectedValue);
        preventive.PreventiveId = _Utility.ConvertToInteger(txtHiddenPreventiveId.Text);
        preventive.BuildingId = Int32.Parse(drlBuilding.SelectedValue);
        if (drlSystem.SelectedIndex > 0 && drlEquipmentType.SelectedIndex != -1)
        {

            EquipmentTypeSystem typeSystem = new EquipmentTypeSystem();
            typeSystem = new ReferenceDataService().GetEquipmentTypeSystem(_Utility.ConvertToInteger(drlSystem.SelectedValue),
                                                                  _Utility.ConvertToInteger(
                                                                      drlEquipmentType.SelectedValue));
            preventive.HaveId = typeSystem.EquipmentTypeSystemId;
                
        }
        preventive.WorkOrderId = _Utility.ConvertToInteger(txtHiddenWOId.Text);
        preventive.StartDate = _Utility.ConvertToCustomizedFormat(txtStartDate.Text);
        preventive.NumberOfMonth = Int32.Parse(txtNumberOfMonth.Text);
        preventive.Description = txtWODescription.Text;

        return preventive;

    }
    protected void btnValidateWorkOrder_Click(object sender, EventArgs e)
    {
        try
        {
            
            Preventive preventive = Getpreventive();
            new PreventiveService().SavePreventive(preventive);
            lblInformation.Text = Resource.MessageValidate;
            return;
        }
        catch (Exception ex)
        {
            lblInformation.Text = ex.Message;
        }

    }
    private void ClearWorkOrderFields()
    {

        drlEmployee.SelectedIndex = -1;
        txtStartDate.Text = string.Empty;
        txtWODescription.Text = string.Empty;
        txtNumberOfMonth.Text = string.Empty;

    }
    protected void btnCancelWorkOrder_Click(object sender, EventArgs e)
    {
        ClearWorkOrderFields();
    }

    protected void btnValidate_Click(object sender, EventArgs e)
    {
       
        try
        {
            
            PreventiveService preventiveService  = new PreventiveService();
            Preventive preventive = Getpreventive();
            preventiveService.SavePreventive(preventive);

            
            // add new category if not exist
            MaintenanceCategory maintenanceCategory = new MaintenanceCategory();
            maintenanceCategory = preventiveService.IsAlreadyExistMaintenanceCategory(Int32.Parse(drlEquipmentType.SelectedValue),
                                                                      txtNewCategory.Text
                );
            if(maintenanceCategory == null)
            {

                maintenanceCategory.EquipmentTypeId = _Utility.ConvertToInteger(drlEquipmentType.SelectedValue);
                maintenanceCategory.Description = txtNewCategory.Text;
                preventiveService.SaveMaintenanceCategory(maintenanceCategory);
            }
            

            lblInformation.Text = Resource.MessageValidate;
            return;
        }
        catch (Exception ex)
        {
            lblInformation.Text = Resource.Permission;
        }
    }
    protected void imgButton_Click(object sender, System.Web.UI.ImageClickEventArgs e)
    {

    }
    public string GetSelectedEmployee()
    {
        if (drlEmployee.SelectedIndex > 0)
             return "assignEmployeeId=" +drlEmployee.SelectedValue;
        return string.Empty;


        
    }
}
